package com.huoyun.car.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huoyun.base.dao.BaseDaoImpl;
import com.huoyun.car.bean.CarPubInfo;
import com.huoyun.car.bean.CarPubInfoCar;
import com.huoyun.car.dao.evt.GetCarPubsByCityEvt;
import com.huoyun.car.dao.evt.QueryCarForGoodsEvt;
import com.huoyun.car.model.CarPubInfoIndexModel;
import com.huoyun.car.model.CarPubInfoModel;
import com.huoyun.car.model.BidCarInfoModel;

public class CarPubInfoDaoImpl extends BaseDaoImpl<CarPubInfo> implements
		CarPubInfoDao {

	@Override
	public void insert(final CarPubInfo record) {
		this.sqlSessionTemplate.insert(
				"com.huoyun.car.dao.CarPubInfoDaoMapper.publish", record);
	}

	@Override
	public List<CarPubInfo> getInfoListByUserId(final int userId) {
		return this.sqlSessionTemplate.selectList(
				"com.huoyun.car.dao.CarPubInfoDaoMapper.getInfoListByUserId",
				userId);
	}

	@Override
	public CarPubInfo getCarPubInfoById(final Long id) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplate.selectOne(
				"com.huoyun.car.dao.CarPubInfoDaoMapper.getCarInfoById", id);
	}

	@Override
	public void updateAll(final CarPubInfo carPubInfo) {
		this.sqlSessionTemplate.update(
				"com.huoyun.car.dao.CarPubInfoDaoMapper.updateAll", carPubInfo);
	}
	
	public List<String> getCarNumberListByUserId(int userId) {
		return this.sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.getCarNumberListByUserId", userId);
	}
	
	public void deleteCarPubInfoById(long carPubInfoId) {
		this.sqlSessionTemplate.delete("com.huoyun.car.dao.CarPubInfoDaoMapper.deleteCarPubInfoById", carPubInfoId);
	}

	@Override
	public List<CarPubInfoCar> getInfoListByCondition(final CarPubInfo condition) {
		return this.sqlSessionTemplate
				.selectList(
						"com.huoyun.car.dao.CarPubInfoDaoMapper.getInfoListByCondition",
						condition);
	}

	@Override
	public List<CarPubInfoModel> getCarPubInfoItemListByUserId(long userId) {
		return this.sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.getCarPubInfoItemListByUserId", userId);
	}

	@Override
	public void updateStatus(long carPubInfoId, int status) {
		// TODO Auto-generated method stub
		Map<String, String> map = new HashMap<String, String>();
		map.put("carPubInfoId", carPubInfoId+"");
		map.put("status", status+"");		
		this.sqlSessionTemplate.update("com.huoyun.car.dao.CarPubInfoDaoMapper.updateStatus", map);
	}

	@Override
	public List<CarPubInfoModel> getCarPubInfoModelListByUsername(String username) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.getCarPubInfoModelListByUsername", username);
	}

	@Override
	public List<CarPubInfoModel> getCarPubInfoModelListByCarNumber(
			String carNumber) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.getCarPubInfoModelListByCarNumber", carNumber);
	}

	@Override
	public List<CarPubInfoModel> getOfflineCarPubInfoModelList(long userId) {
		// TODO Auto-generated method stub
		return this.sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.getOfflineCarPubInfoModelList", userId);
	}
	
	/**
	 * 货主为货物查找相符车辆的数量
	 * 
	 * 与getSelectedCarCount接口一起使用，构造前台展示的分页对象。
	 * @author 刘晓宁
	 * @param queryCarForGoodsEvt 查询条件
	 * @return 相符的记录数量
	 */
	public long queryCarCountForGoods(QueryCarForGoodsEvt queryCarForGoodsEvt)
	{
		return sqlSessionTemplate.selectOne("com.huoyun.car.dao.CarPubInfoDaoMapper.queryCarCountForGoods", queryCarForGoodsEvt);
	}
	
	
	/**
	 * 货主为货物查询相符的车辆列表
	 * 
	 * @author 刘晓宁
	 * @param queryCarForGoodsEvt 查询条件
	 * @return 相符的记录列表
	 */
	public List<BidCarInfoModel> queryCarForGoods(QueryCarForGoodsEvt queryCarForGoodsEvt)
	{
		return sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.queryCarForGoods",queryCarForGoodsEvt);
	}
	
	/**
	 * 查询应标货源的所有车源信息
	 * @author 刘晓宁
	 * @param goodsId 货物id
	 * @return 相符的记录列表
	 */
	public List<BidCarInfoModel> queryBidCars(long goodsId, String[] carIds)
	{
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("goodsId", goodsId);
		dataMap.put("carIds", carIds);
		
		return this.sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.queryBidCars", dataMap);
	}

	/**
	 * 为首页查询车源信息
	 * @author yang
	 */
	@Override
	public List<CarPubInfoIndexModel> getCarPubsByCity(GetCarPubsByCityEvt evt) {
		// TODO Auto-generated method stub
		return sqlSessionTemplate.selectList("com.huoyun.car.dao.CarPubInfoDaoMapper.getCarPubsByCity",evt);
	}
}
